<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ include file="/WEB-INF/jsp/component/common.jsp"%>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge"></meta>
    <title>设备管理</title>
    <link rel="stylesheet" href="${frames}/css/lib/bootstrap.min.css" type="text/css" type="text/css">
    <link rel="stylesheet" href="${frames}/css/lib/dataTables.bootstrap.css" type="text/css">
    <link rel="stylesheet" href="${newframe}/css/base/base.css" type="text/css">
    <link rel="stylesheet" href="${assets}/css/views/department/departmentMgr.css">
    <%@ include file="/WEB-INF/jsp/component/commonBottom.jsp"%>
    <style>
        #upload_instrument {
            position: relative; /* 保证子元素的定位 */
            background: #eee;
            border: 1px solid #ccc;
            text-align: center;
            cursor: pointer;
        }

        #text {
            display: inline-block;
            margin-top: 5px;
            color: #666;
            font-family: "黑体";
            font-size: 18px;
        }

        #file {
            display: block;
            position: absolute;
            top: 0;
            left: 0;
            width: 100%; /* 宽高和外围元素保持一致 */
            height: 100%;
            opacity: 0;
            -moz-opacity: 0; /* 兼容老式浏览器 */
            filter: alpha(opacity = 0); /* 兼容IE */
        }
        .wrap{
            position:relative;
        }
        .loading{
            width:100%;
            height:100%;
            position:absolute;
            left:0;
            top:0;
            /* z-index:-3; */
        }
        .loadingImg{
            width:50px;
            height:50px;
            position:absolute;
            top:330px;
            left:50%;
            margin-left:-25px;
        }
        #mytool{
            float:right
        }
    </style>
    <script>
        function addInstr() {
            window.parent.showNewModelIframe(
                "${URL_WHOSPITAL_TOSAVE}?method=1",750, 400);
        }
    </script>
</head>
<body>

<div class="wrap">
    <%@ include file="./../../breadline.jsp" %>
    <div class="content">
        <h1>报告单医院管理</h1>
        <div class="form">
            <label>医院名称:</label>
            <input id="hospitalName" name="hospitalName" type="text" class="SN" placeholder="请输入医院名称">
        </select>
            <button class="search" id="query" onclick="refreshTable();">搜索</button>
        </div>
        <div id="tablepart">
            <table id="example" class="display" cellspacing="10" width="100%" >
                <thead>
                <tr style="background: #e6e6e6;">
                    <th><input id="all_id" type="checkbox"></th>
                    <th>医院名称</th>
                    <th>医院地址</th>
                    <th>医院编码</th>
                    <th>登陆名</th>
                    <th>密码</th>
                    <th>分区</th>
                </tr>
                </thead>
            </table>
        </div>
    </div>
    <div class="loading" style="display: none;">
        <img src="${newframe }/images/slidebar/loading.gif" class="loadingImg">
    </div>
</div>
<script>
    var table;
    function initTable() {
        table = $('#example').DataTable(
                {
                    "dom" : "<'row'<'#mytool.col-xs-10'><'col-xs-6'f>r>"
                    + "t"
                    + "<'row pageNum'<p>>",
                    "bFilter" : false,//去掉搜索框
                    "sLengthMenu":[10],
                    "bAutoWidth" : true, //自适应宽度
                    "bInfo":false,
                    "sPaginationType" : "full_numbers",
                    "ordering" : false,//是否允许Datatables开启排序
                    "bProcessing" : true,
                    "bServerSide" : true,
                    "sScrollX":"100%",
                    "sAjaxSource" : "${URL_WHOSPITAL_SEARCHWHOSPITAL}",
                    "sAjaxDataProp" : "dataList",//查询后，返回的集合
                    "fnServerData" : function(sSource, aoData, fnCallback) { //查询条件
                        aoData.push({
                            "name" : "hospitalName",
                            "value" : $("#hospitalName").val()
                        })
                        $.ajax({
                            "dataType" : 'json',
                            "url" : sSource,
                            "type" : 'post',
                            "data" : aoData,
                            "success" : function(resp){
                                console.log(resp)
                                fnCallback(resp)
                            },
                        });

                    },
                    "aoColumns" : [
                        {
                            "targets" : 0,
                            "data" : null,
                            "fnCreatedCell" : function(nTd, sData, oData, iRow, iCol) {
                                $(nTd).html(
                                        ''
                                        + '&nbsp;&nbsp;<input name="id" id = "'+oData.id + '" value="'
                                        + oData.id
                                        + '" type="checkbox"><label for='+oData.id+'></label>'
                                        + '');
                            }
                        },
                        {
                            "mDataProp" : "hospitalName"
                        },
                        {
                            "mDataProp" : "address"
                        },
                        {
                            "mDataProp" : "hospitalCode"
                        },
                        {
                            "mDataProp" : "loginName"
                        },
                        {
                            "mDataProp" : "password"
                        },
                        {
                            "mDataProp" : "partition"
                        } ],
                    "oLanguage" : {//插件的汉化
                        "sLengthMenu" : '',
                        "sZeroRecords" : "抱歉， 没有找到",
                        "sInfoEmpty" : "没有数据",
                        "sInfoFiltered" : "(从 _MAX_ 条数据中检索)",
                        "oPaginate" : {
                            "sFirst" : "",
                            "sPrevious" : "<b>上一页<b>",
                            "sLast" : "",
                            "sNext" : "<i>下一页</i>"

                        },
                        "sZeroRecords" : "没有检索到数据",
                        "sProcessing" : "<img src='' />",
                        "sSearch" : "搜索"
                    },
                    "initComplete" : function() {
                        <shiro:hasPermission name="instrument:delete">
                        $("#mytool")
                            .append(
                                '<button id="delete_butt" type="button" onclick="deleteInstr()" style="float:right">删除</button>');
                        </shiro:hasPermission>
                        <shiro:hasPermission name="instrument:update">
                        $("#mytool")
                            .append(
                                '<button id="update_butt" type="button" onclick="updateInstr()" style="float:right">修改</button>');
                        </shiro:hasPermission>
                        <shiro:hasPermission name="instrument:add">
                        $("#mytool")
                            .append(
                                '<button id="add_butt" type="button" onclick="addInstr()" style="float:right">新增</button>');
                        </shiro:hasPermission>
                    }
                });
    }
    function getCheckedArray() {
        var checkedArray = [];
        $("input[type='checkbox']").each(function() {
            if (this.checked) {
                checkedArray.push(this.id);
            }
        });
        return checkedArray;
    }
    $(document).ready(function() {
        initTable();
        var table = $('#example').DataTable();
        var params = {};
        $('#example tbody').on( 'click', 'tr td a', function () {
            var Otr = $(this).parents('td').parents('tr');
            params = table.rows(Otr).data()[0];
            $.ajax({
                url:"${URL_SCREENING_GETBREADLINE}",
                dataType:'json',
                type:'post',
                success:function(data){
                    breadLine = JSON.parse(data.data)
                    var url = '${URL_BASE_INSTRUMENT_TOINFO}?departmentId=' + params.instrumentId
                    breadLine.push({
                        name:'设备详情',
                        href:url
                    })
                    $.ajax({
                        url:'${URL_SCREENING_SAVEBREADLINE}',
                        data:{
                            breadLine:JSON.stringify(breadLine)
                        },
                        dataType:'json',
                        type:'post',
                        success:function(data){
                            parent.showSrc('${URL_BASE_INSTRUMENT_TOINFO}?departmentId=' + params.instrumentId)
                        }
                    })
                }
            })
        });
    });
    function updateInstr() {
        var checkedArray = getCheckedArray();
        if (checkedArray.length != 1) {
            alert("请勾选一条数据修改");
            return;
        }
        let trs = $('tbody tr')
        var id,
            hospitalCode,
            hospitalName,
            address,
            password,
            loginName,
            partition;
        for( let a = 0; a < trs.length; a++ ){
            var tds = $(trs[a]).find('td').eq(3)
            var input = $(trs[a]).find('input')
            for( let i = 0; i < input.length; i++ ){
                if( $(input[i]).attr('id') === checkedArray[0] ){
                    hospitalCode = $(trs[a]).find('td').eq(3).text()
                    hospitalName = $(trs[a]).find('td').eq(1).text()
                    address = $(trs[a]).find('td').eq(2).text()
                    password = $(trs[a]).find('td').eq(5).text()
                    loginName = $(trs[a]).find('td').eq(4).text()
                    partition = $(trs[a]).find('td').eq(6).text()
                }
            }
        }
        var url = encodeURI(encodeURI("${URL_WHOSPITAL_TOSAVE}?id="
            + checkedArray[0] + "&method=2" + '&hospitalCode=' + hospitalCode +'&hospitalName='+ hospitalName +
            '&address=' + address + '&password=' + password + '&loginName=' + loginName + '&partition=' + partition ))
        window.parent.showNewModelIframe(url, 750, 400);
    }
    function deleteInstr() {
        var checkedArray = getCheckedArray();
        var ids = "";
        if (checkedArray.length == 0) {
            alert("请勾选至少一项数据");
            return;
        } else {
            for (var i = 0; i < checkedArray.length; i++) {
                ids += checkedArray[i] + ",";
            }
            ids = ids.substring(0, ids.length - 1);
        }
        var r = confirm("确定要删除勾选的数据吗?");
        if (r == true) {
            $.ajax({
                url : '${URL_WHOSPITAL_DELETE}',//删除地址
                dataType : 'json',
                type : 'post',
                data : {
                    ids : ids
                },
                async : false, //默认为true 异步
                error : function() {
                    alert('error');
                },
                success : function(data) {
                    if (!data.success) {
                        alert("刪除失敗，失敗原因：" + data.msg);
                    }
                    refreshTable();
                }
            });
        }
    }
</script>
</body>
</html>
